package com.shaozi.nc.mq.translation;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.shaozi.data.mq.business.base.MqConsts.UserType;
import com.shaozi.data.mq.business.base.SourceTypeConsts.OA.Task;
import com.shaozi.data.mq.business.bean.MQExchangeData;
import com.shaozi.data.mq.business.bean.MQExchangeRuleBean;
import com.shaozi.data.mq.business.bean.MQPushItem;
import com.shaozi.data.mq.business.bus.MqQuartzSchedule;
import com.shaozi.lib.throwable.SzException;

public class TaskPushTranslation extends BaseOaPushTranslation {

	private Logger logger = Logger.getLogger(TaskPushTranslation.class);
	@Autowired
	private MqQuartzSchedule quartzSchedule;

	public TaskPushTranslation() {

	}

	@Override
	protected void onTranslatePush(MQExchangeData data, int[] userTypes) throws SzException {
		MQExchangeRuleBean rule = (MQExchangeRuleBean) data.getRule();
		MQPushItem pushItem = rule.getPush();
		switch (pushItem.getSourceType()) {
		case Task.DeleteAlarm:
			try {
				quartzSchedule.removeSchedule(data, Task.Alarm);
			} catch (SzException e) {
				logger.error(SzException.getStackMessage(e));
			}
			break;
		default:
			super.onTranslatePush(data, userTypes);
			break;
		}

	}

	protected int[] onUserType(MQExchangeData data) {
		MQExchangeRuleBean rule = (MQExchangeRuleBean) data.getRule();
		MQPushItem pushItem = rule.getPush();
		switch (pushItem.getSourceType()) {
		case Task.Alarm:
		case Task.DeleteAlarm:
		case Task.Del_Task:
			return super.onUserType(data);
		case Task.AddComment:
			int[] userTypes = { UserType.Creator, UserType.Actor, UserType.Principal };
			return userTypes;
		case Task.ReplyComment:
			int[] replyUserTypes = { UserType.Creator, UserType.Actor, UserType.Principal, UserType.Comment };
			return replyUserTypes;
		case Task.Principal:
			int[] principalUserTypes = { UserType.Principal, UserType.User1 };
			return principalUserTypes;
		default:
			int[] defUserTypes = { UserType.Creator, UserType.Actor, UserType.Principal, UserType.Follow };
			return defUserTypes;
		}

	}
}
